package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.po.NewFile;
import com.zhijian.medical.entity.vo.request.NewFileHistorySearchVo;
import com.zhijian.medical.entity.vo.request.NewFileSearchVo;
import com.zhijian.medical.entity.vo.request.NewFileUpdateVo;
import com.zhijian.medical.entity.vo.response.NewFileHistoryVo;
import com.zhijian.medical.entity.vo.response.NewFileVo;
import com.zhijian.medical.service.NewFileHistoryService;
import com.zhijian.medical.service.NewFileService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 新文件管理 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-07-03 15:48:39
 */
@Slf4j
@Controller
@Api(tags = "新文件管理")
public class NewFileController {

    @Autowired
    private NewFileService service;

    @Autowired
    private NewFileHistoryService newFileHistoryService;

    /**
     * 新增
     *
     * @param param
     * @return
     */

    @Response
    @PostMapping("/new/file")
    @ApiOperation("新增")
    public boolean insert(@RequestBody NewFile param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/new/file/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/new/file/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody NewFileUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/new/file/{id}")
    @ApiOperation("根据id查询")
    public NewFileVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/new/file/page")
    @ApiOperation("分页查询")
    public Page<NewFileVo> selectByCondition(@RequestBody(required = false) NewFileSearchVo param) {
        if (param == null)
            param = NewFileSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 修改记录分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/new/file/history/page")
    @ApiOperation("修改记录分页查询")
    public Page<NewFileHistoryVo> selectHistoryByCondition(@RequestBody NewFileHistorySearchVo param) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return newFileHistoryService.selectPage(param);
    }

    /**
     * 废止
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/new/file/discard/{id}")
    @ApiOperation("废止")
    public boolean discard(@PathVariable("id") Long id) {
        return service.discard(id);
    }

    /**
     * 预览
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/new/file/preview/{id}")
    @ApiOperation("预览")
    public void preview(@PathVariable("id") Long id, HttpServletResponse resp) {
        service.preview(id, resp);
    }

    /**
     * 通过文件路径预览
     *
     * @param filePath
     * @return
     */
    @Response
    @GetMapping("/preview")
    @ApiOperation("通过文件路径预览")
    public void preview(@ApiParam(name = "filePath", value = "文件路径") @RequestParam(value = "filePath") String filePath, HttpServletResponse resp) {
        service.preview(filePath, resp);
    }

    /**
     * 文件受控
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/new/file/control/{id}")
    @ApiOperation("文件受控")
    public boolean addWaterMark(@PathVariable("id") Long id) {
        return service.addWaterMark(id);
    }

}